// pages/chat/chat.ts
Page({
    /**
     * 页面的初始数据
     */
    data: {
        translateY: 0,
        KeyboardHeight: 0,
        mode:'audio',
        otherVisible: false,
        sendVisible: false,
        lineHeight: 0,
        increaseHeight: 0,
        msgScrollTop: 0,
        scrollTop: 0,
        msgList: [
            {
                id: '001',
                role: 0,
                senderName: 'lijie',
                type: 0,
                content: 'hello',
                createTime: '2025-02-23 19:02'
            },
            {
                id: '002',
                role: 1,
                type: 0,
                senderName: 'libai',
                content: 'hello',
                createTime: '2025-02-23 19:02'
            },
            {
              id: '002',
              role: 0,
              type: 3,
              senderName: 'libai',
              content: 'hello',
              fileDuration:12,
              createTime: '2025-02-23 19:02'
          }
        ],
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad() {
      const windowInfo = wx.getWindowInfo();
      console.log(windowInfo.safeArea,'000');
        wx.onKeyboardHeightChange(async (e) => {
            // 键盘弹出
            if (e.height) {
                this.setData({
                    KeyboardHeight: e.height
                })
                // this.msgScrollTop = Date.now();
                // 输入框上移动
                this.setData({
                    translateY: e.height
                })
            } else {
                if (!this.data.otherVisible) {
                    this.setData({
                        translateY: 0
                    })
                }
            }
        });
    },
    changeMode(){
      if(this.data.mode === 'text'){
         this.setData({
           mode:'audio'
         })
      }else{
        this.setData({
          autoFocus:true,
          mode:'text'
        })
      }
    },
    textChange(e: any) {
        if (e.detail.value.length) {
            this.setData({
                sendVisible: true,
                msg: e.detail.value
            })
        } else {
            this.setData({
                sendVisible: false,
                msg: ''
            })
        }
    },
    textLineChange(e: any) {
        const { height, lineCount } = e.detail || e.target;
        if (!this.data.lineHeight) this.setData({ lineHeight: height });
        const r = height - this.data.lineHeight;
        // 计算
        this.setData({
            increaseHeight: r > 200 ? 200 : r
        })
        setTimeout(() => {
            this.setData({
                msgScrollTop: Date.now(),
                scrollTop: Date.now()
            })
        })
    },
    showOthers(e) {
        this.setData({
            mode: e.currentTarget.dataset.type,
            otherVisible: true,
            translateY: this.data.KeyboardHeight || 284
        })
        // if (this.otherVisible) {
        // 	// this.otherVisible = false;
        // 	this.autoFocus = true;
        // } else {
        // 	// this.mode = 'text'
        // 	// this.mode = mode;
        // 	this.otherVisible = true;
        // 	this.autoFocus = false;
        // }
    },
    closeHandle() {
        this.setData({
            autoFocus: false,
            otherVisible: false,
            translateY: 0,
        })
        wx.hideKeyboard();
        setTimeout(() => {
            this.setData({
                revokeId: ''
            })
        }, 300);

    },
    sendHandle() {
        // event.preventDefault();
        this.data.msgList.unshift({
            id: '003',
            role: 0,
            type: 0,
            senderName: 'libai',
            content: this.data.msg,
            createTime: '2025-02-23 19:02'
        });
        this.setData({
            msgList: this.data.msgList
        })
        return false;
    }
})